{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Volume Mesh\n",
    "\n",
    "Mesh is an object which displays triangles in 3d.\n",
    "An scalar can be displayed on the mesh using color map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import vtk\n",
    "import k3d\n",
    "import numpy as np\n",
    "import math\n",
    "import ipywidgets as widgets\n",
    "from vtk.util import numpy_support\n",
    "from k3d.helpers import quad, download\n",
    "\n",
    "filename = download('https://vedo.embl.es/examples/data/embryo.slc')\n",
    "reader = vtk.vtkSLCReader()\n",
    "reader.SetFileName(filename)\n",
    "reader.Update()\n",
    "vti = reader.GetOutput()\n",
    "\n",
    "x, y, z = vti.GetDimensions()\n",
    "volume_data = numpy_support.vtk_to_numpy(vti.GetPointData().GetArray(0)).reshape(-1, y, x).astype(np.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "basic_color_maps = [(attr, getattr(k3d.basic_color_maps, attr)) for attr in dir(k3d.basic_color_maps) if not attr.startswith('__')]\n",
    "paraview_color_maps = [(attr, getattr(k3d.paraview_color_maps, attr)) for attr in dir(k3d.paraview_color_maps) if not attr.startswith('__')]\n",
    "matplotlib_color_maps = [(attr, getattr(k3d.matplotlib_color_maps, attr)) for attr in dir(k3d.matplotlib_color_maps) if not attr.startswith('__')]\n",
    "colormaps = basic_color_maps + paraview_color_maps + matplotlib_color_maps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "82c3b23b5e074f81a6c6fff11579af2c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "vertices, indices = quad(20.0, 20.0)\n",
    "\n",
    "plot = k3d.plot()\n",
    "\n",
    "obj = k3d.mesh(vertices, indices, volume=volume_data, side='double', volume_bounds=[-10, 10, -10, 10, -10, 10])\n",
    "\n",
    "model_matrix = {}\n",
    "for t in np.linspace(0, 2* np.pi, 100):\n",
    "    obj.transform.rotation = [t, math.sin(t), math.cos(t), 1]\n",
    "    model_matrix[str(t)] = obj.model_matrix\n",
    "    \n",
    "obj.model_matrix = model_matrix    \n",
    "plot += obj\n",
    "\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = plot.get_binary_snapshot()\n",
    "\n",
    "with open('binary_snapshot.k3d', 'wb') as f:\n",
    "    f.write(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "tf_editor = k3d.transfer_function_editor()\n",
    "\n",
    "@widgets.interact(x=widgets.Dropdown(options=colormaps, description='ColorMap:'))\n",
    "def g(x):\n",
    "    tf_editor.color_map = np.array(x, dtype=np.float32)\n",
    "\n",
    "_ = widgets.link((tf_editor, 'color_map'), (obj, 'color_map'))\n",
    "_ = widgets.link((tf_editor, 'opacity_function'), (obj, 'opacity_function'))\n",
    "\n",
    "tf_editor.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tf_editor.opacity_function = np.array([\n",
    "    0, 0, \n",
    "    0.04, 0,\n",
    "    0.1, 1, \n",
    "    1,1\n",
    "], dtype=np.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.start_auto_play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.stop_auto_play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('./volume.html', 'w', encoding='UTF-8')\n",
    "f.write(plot.get_snapshot(9, 'K3DInstance.startAutoPlay();'))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  },
  "nbTranslate": {
   "displayLangs": [
    "en",
    "pl"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "pl",
   "targetLang": "en",
   "useGoogleTranslate": true
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
